package com.yuda.operationcenter.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.yuda.operationcenter.service.IOperInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yuda.common.log.annotation.Log;
import com.yuda.common.log.enums.BusinessType;
import com.yuda.common.security.annotation.PreAuthorize;
import com.yuda.operationcenter.domain.OperLabel;
import com.yuda.operationcenter.service.IOperLabelService;
import com.yuda.common.core.web.controller.BaseController;
import com.yuda.common.core.web.domain.AjaxResult;
import com.yuda.common.core.utils.poi.ExcelUtil;
import com.yuda.common.core.web.page.TableDataInfo;

/**
 * 标签配置Controller
 *
 * @author wyn
 * @date 2021-04-12
 */
@RestController
@RequestMapping("/label")
public class OperLabelController extends BaseController {
    @Autowired
    private IOperLabelService operLabelService;


    @Autowired
    private IOperInformationService operInformationService;

    /**
     * 查询标签配置列表
     */
    @PreAuthorize(hasPermi = "gwoper:label:list")
    @GetMapping("/list")
    public TableDataInfo list(OperLabel operLabel) {
        startPage();
        List<OperLabel> list = operLabelService.selectOperLabelList(operLabel);
        return getDataTable(list);
    }

    /**
     * 导出标签配置列表
     */
    @PreAuthorize(hasPermi = "gwoper:label:export")
    @Log(title = "标签配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OperLabel operLabel) throws IOException {
        List<OperLabel> list = operLabelService.selectOperLabelList(operLabel);
        ExcelUtil<OperLabel> util = new ExcelUtil<OperLabel>(OperLabel.class);
        util.exportExcel(response, list, "label");
    }

    /**
     * 获取标签配置详细信息
     */
    @PreAuthorize(hasPermi = "gwoper:label:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(operLabelService.selectOperLabelById(id));
    }

    /**
     * 新增标签配置
     */
    @PreAuthorize(hasPermi = "gwoper:label:add")
    @Log(title = "标签配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OperLabel operLabel) {
        if (operLabelService.insertOperLabel(operLabel) == -1) {
            return AjaxResult.error("标签已经存在");
        }
        return AjaxResult.success();
    }

    /**
     * 修改标签配置
     */
    @PreAuthorize(hasPermi = "gwoper:label:edit")
    @Log(title = "标签配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OperLabel operLabel) {
        int res = operLabelService.updateOperLabel(operLabel);
        if (res == -1) {
            return AjaxResult.error("标签已经存在");
        }

        if (res == -2) {
            return AjaxResult.error("标签正在使用");
        }
        return AjaxResult.success();
    }

    /**
     * 删除标签配置
     */
    @PreAuthorize(hasPermi = "gwoper:banner:remove")
    @Log(title = "标签配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        //判断标签有没有使用过
        boolean used = operInformationService.checkIsUsed(ids);
        if (used) {
            return AjaxResult.error("暂时不能删除，有标签在使用");
        }
        return toAjax(operLabelService.deleteOperLabelByIds(ids));
    }
}
